﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myCommon.Utils
{
    public static class Utils
    {

        /// <summary>
        /// 16进制格式字符串转字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] ToBytesFromHexString(this string hexString)
        {
            //以 ' ' 分割字符串，并去掉空字符
            string[] chars = hexString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            byte[] returnBytes = new byte[chars.Length];
            //逐个字符变为16进制字节数据
            for (int i = 0; i < chars.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(chars[i], 16);
            }
            return returnBytes;
        }

        public static string GenerateSaveName(string rootDir, string userID, string videoID, string ext)
        {
            string date_time = System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
            Console.WriteLine(date_time);
            System.Collections.Generic.List<char> charList = new System.Collections.Generic.List<char>();
            charList.Add('/');
            charList.Add(':');
            charList.Add('\\');
            charList.Add('>');
            charList.Add('<');
            charList.Add('?');
            charList.Add('*');
            charList.Add('"');
            charList.Add('|');
            //string userID = contex.userID;
            if (userID == null)
            {
                userID = "combine";
            }
            else
            {
                foreach (var c in charList)
                {
                    userID = userID.Replace(c, '_');
                }
            }

            //string userID = contex.userID.Replace('/', '_').Replace(':', '_').Replace('\\', '_').Replace('*','_').Replace();
            string FileName = System.IO.Path.Combine(
                rootDir,
                userID + "_" + videoID + (videoID == "" ? "" : "_") + date_time + ext
                );
            return FileName;
        }
    }
}
